package com.joshua.viewtest.demo.cardview;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.joshua.viewtest.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CardViewTest1 extends AppCompatActivity {
    @BindView(R.id.corner_radius_seek_bar)
    SeekBar radiusSeekBar;
    @BindView(R.id.elevation_seek_bar)
    SeekBar elevationSeekBar;
    @BindView(R.id.alpha_seek_bar)
    SeekBar alphaSeekBar;
    @BindView(R.id.select_bg_color_radio)
    RadioGroup radioGroup;

    @BindView(R.id.cardView)
    CardView cardView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_view_test1);
        ButterKnife.bind(this);

        radiusSeekBar.setProgress((int) cardView.getRadius());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            elevationSeekBar.setProgress((int) cardView.getElevation());
        }
        alphaSeekBar.setProgress((int) (ViewCompat.getAlpha(cardView) * 255));

        MySeekBarChangedListener listener = new MySeekBarChangedListener();
        radiusSeekBar.setOnSeekBarChangeListener(listener);
        elevationSeekBar.setOnSeekBarChangeListener(listener);
        alphaSeekBar.setOnSeekBarChangeListener(listener);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int id) {
                switch (id) {
                    case R.id.red:
                        cardView.setBackgroundResource(android.R.color.holo_red_light);
                        break;
                    case R.id.blue:
                        cardView.setBackgroundResource(android.R.color.holo_blue_light);
                        break;
                    case R.id.green:
                        cardView.setBackgroundResource(android.R.color.holo_green_light);
                        break;
                    default:
                        cardView.setBackgroundResource(R.color.cardview_light_background);
                        break;
                }
            }
        });
    }

    class MySeekBarChangedListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            switch (seekBar.getId()) {
                case R.id.corner_radius_seek_bar:
                    cardView.setRadius(seekBar.getProgress());
                    break;
                case R.id.elevation_seek_bar:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        cardView.setElevation(seekBar.getProgress());
                    }
                    break;
                case R.id.alpha_seek_bar:
                    ViewCompat.setAlpha(cardView, seekBar.getProgress() / 255f);
                    break;
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    }

}
